package com.ktjy.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.crm.entity.Role;
import com.ktjy.crm.entity.User;
import com.ktjy.crm.service.IRoleService;
import com.ktjy.crm.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.java.Log;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Resource
    private IUserService sysUserservice;
    @Resource
    private IRoleService roleService;
    @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, HttpServletRequest request){
        User sysUser = sysUserservice.login(usrName, usrPassword);
        if (sysUser!=null){
            request.getSession().setAttribute("loginUser",sysUser);
            return "main";
        }else {
            request.setAttribute("message","登录失败，用户名或密码错误！");
            return "login";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "login";
    }
    @GetMapping(value = "/user/list")
    public String findUsers(Model model, String usrName,
                            @RequestParam(required = false, defaultValue = "0")Long roleId,
                            @RequestParam(required = false, defaultValue = "1")int pageIndex){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("usrName",usrName);
        hashMap.put("roleId",roleId);
        hashMap.put("pageIndex",pageIndex);
        hashMap.put("pageSize",3);
        IPage<User> userPage = sysUserservice.getUserByPage(hashMap);
        model.addAttribute("userPager",userPage);
        model.addAttribute("usrName",usrName);
        model.addAttribute("roleId",roleId);
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles",roles);
        return "user/list";
    }

    @GetMapping(value = "/user/add")
    public String addUser(Model model){
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles",roles);
        return "user/add";
    }

    @PostMapping("/user/add")
    public String addUser(User sysUser, Model model){
        int result = sysUserservice.addUser(sysUser);
        if (result>0){
            return "redirect:/user/list";
        }else {
            return "redirect:/user/add";
        }
    }

    @GetMapping(value = "/user/edit")
    public String editUser(Model model, int usrId){
        User sysUser = sysUserservice.getUser(usrId);
        model.addAttribute("user",sysUser);
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles",roles);
        return "user/edit";
    }
    @PostMapping("/user/edit")
    public String editUser(User sysUser, Model model){
        int result = sysUserservice.updateUser(sysUser);
        if (result>0){
            return "redirect:/user/list";
        }else {
            return "redirect:/user/edit";
        }
    }

    @RequestMapping(value = "/user/del")
    @ResponseBody
    public Map deleteUser(int usrId) {
        int result = sysUserservice.deleteUser(usrId);
        Map map = new HashMap();
        if (result>0){
            map.put("delResult",true);
        }else {
            map.put("delResult",false);
        }
        return map;
    }
}
